/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.banner.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.banner.vo.BizBannerVO;
import vip.xiaonuo.common.config.ConfigProperties;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.banner.entity.BizBanner;
import vip.xiaonuo.biz.modular.banner.mapper.BizBannerMapper;
import vip.xiaonuo.biz.modular.banner.param.BizBannerAddParam;
import vip.xiaonuo.biz.modular.banner.param.BizBannerEditParam;
import vip.xiaonuo.biz.modular.banner.param.BizBannerIdParam;
import vip.xiaonuo.biz.modular.banner.param.BizBannerPageParam;
import vip.xiaonuo.biz.modular.banner.service.BizBannerService;
import vip.xiaonuo.dev.api.DevConfigApi;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * BIZ_BANNERService接口实现类
 *
 * @author levin
 * @date 2023/02/22 13:11
 **/
@Service
public class BizBannerServiceImpl extends ServiceImpl<BizBannerMapper, BizBanner> implements BizBannerService {
    @Resource
    private DevConfigApi devConfigApi;

    @Override
    public Page<BizBanner> page(BizBannerPageParam bizBannerPageParam) {
        QueryWrapper<BizBanner> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(bizBannerPageParam.getSearchKey())) {
            queryWrapper.lambda().like(BizBanner::getTitle, bizBannerPageParam.getSearchKey());
        }
        if (ObjectUtil.isAllNotEmpty(bizBannerPageParam.getSortField(), bizBannerPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizBannerPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizBannerPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizBannerPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BizBanner::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public void add(BizBannerAddParam bizBannerAddParam) {
        BizBanner bizBanner = BeanUtil.toBean(bizBannerAddParam, BizBanner.class);
        this.save(bizBanner);
    }

    @Override
    public void edit(BizBannerEditParam bizBannerEditParam) {
        BizBanner bizBanner = this.queryEntity(bizBannerEditParam.getId());
        BeanUtil.copyProperties(bizBannerEditParam, bizBanner);
        this.updateById(bizBanner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizBannerIdParam> bizBannerIdParamList) {
        // 执行删除
        this.removeBatchByIds(CollStreamUtil.toList(bizBannerIdParamList, BizBannerIdParam::getId));
    }

    @Override
    public BizBanner detail(BizBannerIdParam bizBannerIdParam) {
        return this.queryEntity(bizBannerIdParam.getId());
    }

    @Override
    public BizBanner queryEntity(String id) {
        BizBanner bizBanner = this.getById(id);
        if (ObjectUtil.isEmpty(bizBanner)) {
            throw new CommonException("BIZ_BANNER不存在，id值为：{}", id);
        }
        return bizBanner;
    }

    @Override
    public List<BizBannerVO> getListByType(Integer type) {
        String baseUrl = devConfigApi.getValueByKey("BACKEND_BASE_URL");
        if (ObjectUtil.isEmpty(baseUrl)) {
            throw new CommonException("服务端URL未正确配置");
        }
        if (null == type) {
            throw new CommonException("缺少type参数");
        }
        List<BizBannerVO> result = new ArrayList<>();
        QueryWrapper<BizBanner> queryWrapper = new QueryWrapper<>();
        List<Integer> applSceneList = Arrays.asList(0, type);
        queryWrapper.lambda().in(BizBanner::getApplScene, applSceneList);
        queryWrapper.lambda().orderByAsc(BizBanner::getSortCode);

        List<BizBanner> list = this.baseMapper.selectList(queryWrapper);
        for (BizBanner item : list) {
            BizBannerVO bannerVO = BeanUtil.toBean(item, BizBannerVO.class);
            bannerVO.setImgUrl(baseUrl + "/dev/file/download?id=" + item.getImgId());
            result.add(bannerVO);
        }
        return result;
    }
}
